package Mycode.Class053;
class Leetcode2289 {
    private static final int maxn = 100009;
    private static int[][] stack = new int[maxn][2];
    private static int r;
    public int totalSteps(int[] nums) {
        return compute(nums);
    }
    private static int compute(int[] nums) {
        r = 0;
        int ans = 0;
        for (int curTurns, i = nums.length - 1; i >= 0; i --) {
            curTurns = 0;
            while (r > 0 && stack[r - 1][0] < nums[i]) {
                curTurns = Math.max(curTurns + 1, stack[--r][1]);
                ans = Math.max(ans, curTurns);
            }
            stack[r][1] = curTurns;
            stack[r++][0] = nums[i];
        }
        return ans;
    }

}
